package com.qf.commerce.controller;

import com.github.pagehelper.PageInfo;
import com.qf.commerce.common.dto.RoleDto;
import com.qf.commerce.common.vo.AccountVo;
import com.qf.commerce.common.vo.RoleVo;
import com.qf.commerce.entity.Role;
import com.qf.commerce.resp.ResponseResult;
import com.qf.commerce.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/Role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @PostMapping("/add")
    public ResponseResult<Integer> add(@RequestBody RoleDto roleDto){
        try {
            return ResponseResult.success(roleService.add(roleDto));
        } catch (Exception e) {
            return ResponseResult.fail();
        }

    }

    @DeleteMapping("/delete")
    public ResponseResult<Integer> delete(@RequestParam List<Long> ids){
        try {
            return ResponseResult.success(roleService.deleteRole(ids));
        } catch (Exception e) {
            return ResponseResult.fail();
        }

    }

    @PutMapping("/updatePower")
    public ResponseResult<Integer> updatePower(@RequestParam Long parentPowerId, @RequestParam Integer level, @RequestParam Long roleId){
        try {
            return ResponseResult.success(roleService.updatePower(parentPowerId,level,roleId));
        } catch (Exception e) {
            return ResponseResult.fail();
        }

    }
    @PutMapping("/update")
    public ResponseResult<Integer> updateRole(@RequestBody Role role){
        try {
            return ResponseResult.success(roleService.updateRole(role,role.getRoleId()));
        } catch (Exception e) {
            return ResponseResult.fail();
        }

    }


    @GetMapping("/listDetail")
    public ResponseResult<List<RoleDto>> listDetail(@RequestParam Long roleId){
        try {
            return ResponseResult.success(roleService.selectRoleDetail(roleId));
        } catch (Exception e) {
            return ResponseResult.fail();
        }
    }

    @GetMapping("/list")
    public ResponseResult<PageInfo<RoleVo>> list(String organizationName, Integer status, String roleName, String userName, Integer page, Integer size) {
        try {
            return ResponseResult.success(roleService.selectRole(organizationName,status,roleName,userName,page,size));
        } catch (Exception e) {
            return ResponseResult.fail();
        }

    }

}
